<section id="widget-grid" class="">
    <div class="row">
        <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="widget-body">
                <!-- Dynamic Modal -->
                <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <!-- content will be filled here from "ajax/modal-content/model-content-1.html" -->
                        </div>
                    </div>
                </div>
            <a href="javascript:void(0);" onclick="add_software()" id="addsoftware" class="btn btn-success" data-toggle="modal" data-target="#addModal">新增</a> 
			<a href="javascript:void(0);" class="btn btn-primary" onclick="start_software()">启用</a> 
			<a href="javascript:void(0);" class="btn btn-warning" onclick="stop_software()">停用</a>
			<a	href="javascript:void(0);" class="btn btn-danger" onclick="delete_software()">删除</a> <br> <br>
            </div>
        </article>
    </div>
    <div class="row">
    	<div id="list-alert">
        </div>
    </div>
    <div class="row">
        <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <!-- Widget ID (each widget will need unique ID)-->
            <!-- end widget -->
            <table id="jqgrid"></table>
            <div id="pjqgrid"></div>
        </article>
    </div>
</section>
<div class="modal fade" id="taskModal" tabindex="-1" role="dialog" aria-labelledby="taskModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 100%;" id="dialogmodal">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="taskModalLabel">
                   任务状态提示
                </h4>
            </div>
            <div class="modal-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>所属者</th>
                            <th>开始时间</th>
                            <th>结束时间</th>
                            <th>对象类型</th>
                            <th>对象名称</th>
                            <th>任务类型</th>
                            <th>状态</th>
                            <th width="30%">回执信息</th>
                        </tr>
                        </thead>
                        <tbody id="mainframestate">
                        </tbody>
                    </table>

                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<script>
	pageSetUp();
	loadScript("js/plugin/jqgrid/grid.locale-en.min.js", function() {
		loadScript("js/plugin/jqgrid/jquery.jqGrid.min.js", function() {
			var siteid=getSession("siteId");
			sendGet("/upm_manager/v1.0/softwares/images?site="+siteid,createGrid_si_1,ListAlert,null);
		});
	});
	// load related plugins
	var siteid=getSession("siteId");
	function add_software() {
		$("#addsoftware").attr("href", "views/resource/software_image/add.html");
	}
	function delete_software() {
		ListAlertClear();
		var s = $("#jqgrid").jqGrid('getGridParam', 'selarrrow'); 
    	if(s.length==1){
    		 var image_id = $("#jqgrid").jqGrid('getRowData',s).id;
    		 ListConfirm('delete', image_id);
    	 }
    	 else{
    		 ListAlert("请勾选一行数据");
    	 }
	}
	function start_software() {
		ListAlertClear();
		var s = $("#jqgrid").jqGrid('getGridParam', 'selarrrow'); 
    	if(s.length==1){
    		 var image_id = $("#jqgrid").jqGrid('getRowData',s).id;
    		 ListConfirm('start', image_id);
    	 }
    	 else{
    		 ListAlert("请勾选一行数据");
    	 }
	}
	function stop_software() {
		ListAlertClear();
		var s = $("#jqgrid").jqGrid('getGridParam', 'selarrrow'); 
    	if(s.length==1){
    		 var image_id = $("#jqgrid").jqGrid('getRowData',s).id;
    		 ListConfirm('stop', image_id);
    	 }
    	 else{
    		 ListAlert("请勾选一行数据");
    	 }
	}
	
    function confirm(type, id){
    	switch(type){
    	case 'start':
    		sendPut("/upm_manager/v1.0/softwares/images/"+id+"/enable",showReturn,ListAlert,null,null);
    		break;
    	case 'stop':
   		 	sendPut("/upm_manager/v1.0/softwares/images/"+id+"/disable",showReturn,ListAlert,null,null);
    		break;
    	case 'delete':
    		sendDelete("/upm_manager/v1.0/softwares/images/"+id, deleteReturn, ListAlert, null);
    		break;
    	default:
    	}
    }
	
	  function showReturn(data){
			sendGet("/upm_manager/v1.0/softwares/images?site="+siteid,reloadGrid,ListAlert,null);
		}
	  function reloadGrid(data){
			ListAlertClear();
			var jq_data=data['data'];
			$("#jqgrid").clearGridData().setGridParam({data:jq_data}).trigger("reloadGrid");
	    }
	  function deleteReturn(data){
		  sendGet("/upm_manager/v1.0/softwares/images?site="+getSession("siteId"),reloadGrid,ListAlert,null);
	  }
	  
	  $('#taskModal').on("show.bs.modal", function(e){
	    	ListAlertClear();
	    	$("#mainframestate").empty();
	    	sendGet("/upm_manager/v1.0/tasks?obj_id="+$(e.relatedTarget).data('taskId'),createGrid_taskDetail,ListAlert,null);
	  });
	  
	  clearInterval(flag);
	  var flag = setInterval(showReturn, setTime);
</script>